class String
  
  # return true iff string commences with target
  def begins_with(target)
    self[0,target.size]==target
  end

  # return true iff string ends with target
  def ends_with(target)
    target_size = target.size
    target==self[-target_size,target_size]
  end

  # return true iff string ends with target
  # Case insensitive version.  
  def case_ends_with(target)
    target_size = target.size
    target.upcase==self.upcase[-target_size,target_size]
  end

  # iff string begins_with prefix, replace it with new_prefix
  def substitute_prefix!(prefix,new_prefix)
    return self if prefix != self[0,prefix.size]
    self[0,prefix.size] = new_prefix
    self
  end

  # iff string begins_with prefix, return a clone on which the
  # new_prefix has been substituted for prefix
  def substitute_prefix(prefix,new_prefix)
    result =self.clone
    result.substitute_prefix!(prefix,new_prefix)
  end

  # returns substring to the righthand side of the rightmost occurrence of token
  def rightmost_of_token(token) 
     rightmost_index = rindex(token)
     rightmost_index.nil? ? '' : self[rightmost_index+1,size-rightmost_index]
  end
end
